﻿package framework.util.log
{
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	
	
	public class OnlineLogger 
	{
		private var _text:TextField = null;
		private var _sprite:Sprite = null;
		private var _tfm:TextFormat = null;
		private var _content:String = "";				// 文字内容
		
		
		/**
		 * 初始化
		 * @param	stage
		 */
		public function init(stage:Stage):void
		{
			
			_tfm = new TextFormat("Candara", 12, 0xFFFF00);
			_text = new TextField();
			_text.defaultTextFormat = _tfm;
			_text.width = stage.stageWidth;
			_text.height = stage.stageHeight;
			_text.mouseEnabled = false;
			_text.wordWrap = true;
			
			_sprite = new Sprite();
			_sprite.graphics.beginFill(0xaaaaaa, .6);
			_sprite.graphics.drawRect(0, 0, stage.stageWidth , stage.stageHeight);
			_sprite.graphics.endFill();
			
			_sprite.addChild(_text);
			
			_sprite.mouseEnabled = false;	
			stage.addChild(_sprite);
			
			stage.addEventListener(KeyboardEvent.KEY_UP , onKeyUp);
			_sprite.visible = false;
		}
		
		
		private function onKeyUp(e:KeyboardEvent):void 
		{
			if (e.keyCode == Keyboard.DELETE)
			{
				_text.htmlText = "";
				_content = "";
				_text.scrollV = 0;
				return;
			}
			
			if (e.keyCode != Keyboard.ESCAPE)
			{
				return;
			}
			_sprite.visible = !_sprite.visible;
		}
		
		/**
		 * 输出,可多选参数
		 * @param	...arg
		 */
		public function log(...arg):void
		{
			var _date:Date = new Date();
			_content += "[" + _date.getHours() + ":" + _date.getMinutes() + ":" + _date.getSeconds() + "]  " + arg + "\n";
			
			_text.htmlText = _content;
			
			_text.scrollV = _text.maxScrollV;
		}
		
	}
}